<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>智慧童话世界</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            margin: 0;
            padding: 20px;
            min-height: 100vh;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            padding: 30px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
        }
        .header {
            text-align: center;
            margin-bottom: 30px;
        }
        .title {
            color: #5a67d8;
            font-size: 2.5em;
            margin-bottom: 10px;
        }
        .subtitle {
            color: #666;
            font-size: 1.2em;
        }
        .story-area {
            background: #f7fafc;
            border-radius: 10px;
            padding: 20px;
            margin: 20px 0;
            min-height: 200px;
        }
        .choices {
            margin: 20px 0;
        }
        .choice-btn {
            display: block;
            width: 100%;
            padding: 15px;
            margin: 10px 0;
            background: #5a67d8;
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 1.1em;
            cursor: pointer;
            transition: all 0.3s;
        }
        .choice-btn:hover {
            background: #4c51bf;
            transform: translateY(-2px);
        }
        .input-area {
            margin: 20px 0;
        }
        .input-group {
            margin: 15px 0;
        }
        .input-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #333;
        }
        .input-group input, .input-group select {
            width: 100%;
            padding: 10px;
            border: 2px solid #e2e8f0;
            border-radius: 5px;
            font-size: 1em;
        }
        .start-btn {
            background: #48bb78;
            color: white;
            padding: 15px 30px;
            border: none;
            border-radius: 8px;
            font-size: 1.2em;
            cursor: pointer;
            width: 100%;
        }
        .start-btn:hover {
            background: #38a169;
        }
        .features {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin: 30px 0;
        }
        .feature {
            text-align: center;
            padding: 20px;
            background: #f7fafc;
            border-radius: 10px;
        }
        .feature-icon {
            font-size: 2em;
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1 class="title">🌟 智慧童话世界</h1>
            <p class="subtitle">AI可选分支剧情童话书应用</p>
        </div>

        <div class="features">
            <div class="feature">
                <div class="feature-icon">🤖</div>
                <h3>AI智能生成</h3>
                <p>基于ERNIE-4.5大模型</p>
            </div>
            <div class="feature">
                <div class="feature-icon">🧠</div>
                <h3>科研数据</h3>
                <p>智源研究院深度知识</p>
            </div>
            <div class="feature">
                <div class="feature-icon">🎨</div>
                <h3>多模态交互</h3>
                <p>文本/图像/语音体验</p>
            </div>
            <div class="feature">
                <div class="feature-icon">📚</div>
                <h3>个性化学习</h3>
                <p>专属定制童话故事</p>
            </div>
        </div>

        <div class="input-area">
            <h2>🎭 开始你的童话之旅</h2>
            
            <div class="input-group">
                <label for="child-name">小朋友的名字:</label>
                <input type="text" id="child-name" placeholder="请输入小朋友的名字" value="小明">
            </div>
            
            <div class="input-group">
                <label for="age-group">年龄段:</label>
                <select id="age-group">
                    <option value="3-5">3-5岁</option>
                    <option value="6-8" selected>6-8岁</option>
                    <option value="9-12">9-12岁</option>
                </select>
            </div>
            
            <div class="input-group">
                <label for="theme">故事主题:</label>
                <select id="theme">
                    <option value="勇敢与友谊">勇敢与友谊</option>
                    <option value="神奇的森林">神奇的森林</option>
                    <option value="海底探险">海底探险</option>
                    <option value="太空旅行">太空旅行</option>
                    <option value="动物王国">动物王国</option>
                </select>
            </div>
            
            <div class="input-group">
                <label for="interests">兴趣爱好 (用逗号分隔):</label>
                <input type="text" id="interests" placeholder="例如: 动物, 科学, 冒险" value="动物, 冒险, 科学">
            </div>
            
            <button class="start-btn" onclick="startStory()">🚀 开始生成专属童话</button>
        </div>

        <div class="story-area" id="story-content" style="display: none;">
            <h2 id="story-title">故事标题</h2>
            <div id="story-text">故事内容将在这里显示...</div>
        </div>

        <div class="choices" id="story-choices" style="display: none;">
            <h3>🔀 请选择接下来的剧情:</h3>
            <div id="choice-buttons"></div>
        </div>
    </div>

    <script>
        async function startStory() {
            const childName = document.getElementById('child-name').value;
            const ageGroup = document.getElementById('age-group').value;
            const theme = document.getElementById('theme').value;
            const interests = document.getElementById('interests').value.split(',').map(s => s.trim());
            
            if (!childName) {
                alert('请输入小朋友的名字');
                return;
            }
            
            try {
                const response = await fetch('/start_story', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        child_name: childName,
                        age_group: ageGroup,
                        theme: theme,
                        interests: interests
                    })
                });
                
                const result = await response.json();
                
                if (result.success) {
                    displayStory(result.story);
                } else {
                    alert('故事生成失败: ' + result.error);
                }
            } catch (error) {
                alert('网络错误: ' + error.message);
            }
        }
        
        function displayStory(story) {
            document.getElementById('story-title').textContent = story.title;
            document.getElementById('story-text').textContent = story.content;
            document.getElementById('story-content').style.display = 'block';
            
            if (story.choices && story.choices.length > 0) {
                displayChoices(story.choices, story.id);
            }
        }
        
        function displayChoices(choices, storyId) {
            const choiceButtons = document.getElementById('choice-buttons');
            choiceButtons.innerHTML = '';
            
            choices.forEach((choice, index) => {
                const button = document.createElement('button');
                button.className = 'choice-btn';
                button.textContent = choice;
                button.onclick = () => makeChoice(storyId, choice);
                choiceButtons.appendChild(button);
            });
            
            document.getElementById('story-choices').style.display = 'block';
        }
        
        async function makeChoice(storyId, choice) {
            try {
                const response = await fetch('/make_choice', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        story_id: storyId,
                        choice: choice
                    })
                });
                
                const result = await response.json();
                
                if (result.success) {
                    displayStory(result.next_story);
                } else {
                    alert('选择处理失败: ' + result.error);
                }
            } catch (error) {
                alert('网络错误: ' + error.message);
            }
        }
    </script>
</body>
</html>